from rest_framework import serializers

from accounts.serializers import UserSeri
from common.serializers import ExcludeableSeri
from school.serializers import SchoolSeri
from student.serializers import SClassSerializer
from .models import *


class TeacherSeri(ExcludeableSeri):
    me = UserSeri(context={'excludes': ['teacher', 'student']}, required=False)
    teacher_schools = SchoolSeri(many=True, required=False)
    my_classes = serializers.SerializerMethodField('teacher_fat_classes', required=False)

    def create(self, validated_data):
        return Teacher(**validated_data).save()

    def update(self, instance, validated_data):
        instance.base_salary = validated_data.get('base_salary', instance.base_salary)
        instance.percentage_salary = validated_data.get('percentage_salary', instance.percentage_salary)
        instance.per_salary = validated_data.get('per_salary', instance.per_salary)
        instance.fee_per_student = validated_data.get('fee_per_student', instance.fee_per_student)
        instance.fee_per_class = validated_data.get('fee_per_class', instance.fee_per_class)
        instance.save()
        return instance

    def teacher_fat_classes(self, teacher):
        if hasattr(teacher, 'teacher_classes'):
            return SClassSerializer(teacher.teacher_classes, many=True).data
        else:
            return []

    class Meta:
        model = Teacher
        fields = ('pk', 'me', 'teacher_schools', 'my_classes', 'base_salary', 'fee_per_student', 'fee_per_class', 'percentage_salary', 'per_salary')
